package com.vchello.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.vchello.bean.message.Message;
import com.vchello.bean.user.User;
import com.vchello.controller.BaseController;
import com.vchello.service.message.MessageService;
import com.vchello.util.Utility;

/**
 * 
 * @author power
 *
 */
@Controller
@RequestMapping(value = "/admin/message")
public class MessageAdminController extends BaseController {
	
	private static final int STATUS = 1;
	private static final int ISREAD = 1;
	private static final int ISSUCCESS = 1;
	@Autowired
	private MessageService messageService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model, Page<Message> page, Message message){
		page = messageService.selectPage(page, new EntityWrapper<Message>(message));
		model.addAttribute("pages", page);
		return "admin/message/messageList";
		
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public String listAjax(Model model, Page<Message> page, Message message){
		page = messageService.selectPage(page, new EntityWrapper<Message>(message));
		model.addAttribute("pages", page);
		return "admin/message/query/messageQuery";
		
	}
	
	@RequestMapping(value = "/addMessage", method = RequestMethod.GET)
	public String addMessage(){
		return "admin/message/createMessage";
		
	}
	@RequestMapping(value = "/addMessage", method = RequestMethod.POST)
	public void addMessage(Message message){
		User user = getUser();
		boolean isTure = messageService.insert(new Message(user.getId(), 0, message.getTitle(), STATUS, ISREAD, ISSUCCESS, message.getContent(), Utility.getSimpleFormatedDayNow()));
		if(isTure) {
			success("发送消息成功");
			return;
		}
		error("发送消息失败");
		
	}
	
	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	public String detail(Model model, @PathVariable int id){
		Message message = messageService.findById(id);
		model.addAttribute("message", message);
		return "admin/message/details";
		
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public void delete(int id){
		Message message = messageService.findById(id);
		if(null == message) {
			error("找不到你要删除的消息");
			return;
		}
		
		boolean isTrue = messageService.deleteMessage(message);
		if(isTrue) {
			success("删除消息成功");
			return;
		}
		error("删除消息失败");
	}
}
